package grobit.torrent

import grobit.bencode.Bencoder
import grobit.http.HttpEngine
import grobit.tracker.TrackerRequest
import grobit.tracker.TrackerResponse
import grobit.wire.PeerConnection
import org.springframework.core.io.ClassPathResource

input = new BufferedInputStream(new ClassPathResource('pi.torrent').getInputStream())
MetaInfo metaInfo = MetaInfo.createFromMap(Bencoder.decodeNext(input))
input.close()

println metaInfo
println metaInfo.getTracker('http')

tracker = new TrackerRequest(
        [
                tracker: metaInfo.getTracker('http'),
                info_hash: metaInfo.infoHash,
                port: 12345,
                left: metaInfo.totalSize //, compact: false
        ]
)
println tracker.getRequest()

String response = new HttpEngine().process(tracker.getRequest())
TrackerResponse t =  TrackerResponse.createFromMap(Bencoder.decodeNext(new ByteArrayInputStream(response.getBytes('ISO-8859-1'))))

ConnectionManager manager = new ConnectionManager(metaInfo: metaInfo)
manager.processTrackerResponse(t)
println 'Processed all connections.'
